import path from "path";
import fs from "fs";

const tsArr = [];
const getAllTs = (dir) => {
  const list = fs.readdirSync(dir);
  list.forEach((val) => {
    const cur = path.join(dir, val);
    if (fs.statSync(cur).isDirectory()) getAllTs(cur);
    else if (/^[^\.]*\.ts$/.test(cur)) tsArr.push(cur);
  });
};

getAllTs(path.resolve("./src/first"));

// console.log(tsArr);

const getEntries = (arr) => {
  const obj = {};
  arr.map((val) => {
    const entryName = path.basename(val, ".ts");
    // console.log(val, entryName);
    obj[entryName] = val;
  });
  return obj;
};

const entry = getEntries(tsArr);

// console.log(entry, tsArr);
// const filteredJs = {};

const convertRelative = (obj) => {
  const res = {};
  for (let key in obj) {
    // if (key !== "index") {
    //   const ts = path.relative(path.resolve("./src"), obj[key]);
    //   filteredJs[ts.substring(0, ts.length - 3)] = obj[key];
    //   continue;
    // }
    const ts = path.relative(path.resolve("./src"), obj[key]);
    res[ts.substring(0, ts.length - 3)] = obj[key];
  }

  return res;
};

//-------------------------------------------------
const mkdirs = (location) => {
  if (fs.existsSync(location)) return;
  mkdirs(path.dirname(location));
  fs.mkdirSync(location);
};
//-------------------------------------------------
const find = (folder, banList, filter, callback) => {
  let arr = []; // all the files in a array

  const iterateDir = (base) => {
    const curDir = fs.readdirSync(base);
    curDir.forEach((val) => {
      const entry = path.join(base, val);
      if (fs.statSync(entry).isDirectory()) {
        if (banList.indexOf(val) === -1) iterateDir(entry);
      } else {
        arr.push({
          name: val,
          fullPath: entry,
        });
      }
    });
  };

  iterateDir(folder);

  callback(arr.filter(filter).map((val) => val.fullPath));
};

find(
  "./src/first/",
  [".git", "node_modules", "dist"],
  (val) => {
    switch (val.name.substring(val.name.lastIndexOf(".") + 1)) {
      case "css":
      case "png":
      case "jpg":
      case "gif":
      case "mp4":
      case "mp3":
      case "html":
      case "js":
        return true;
      default:
        return false;
    }
  },
  (arr) => {
    arr.forEach((val) => {
      const location = path.join("dist", path.relative("src", val));
      mkdirs(path.dirname(path.resolve(location)));
      fs.copyFileSync(val, location);
    });
  }
);
//-------------------------------------------------
const entries = convertRelative(entry);
// for (const key in filteredJs) {
//   const tmp = path.join("dist", key) + ".js";
//   mkdirs(path.dirname(tmp));
//   fs.copyFileSync(filteredJs[key], tmp);
//   console.log(`from ${filteredJs[key]} to ${tmp}`);
// }
console.log(entries);

export default {
  entry: entries,
  target: "node",
  output: {
    path: path.resolve("./", "dist"), // Output directory
    libraryTarget: "amd",
    environment: {
      arrowFunction: false,
    },
  },
  resolve: {
    extensions: [".ts", ".js"], // Include TypeScript and JavaScript files
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
};
